/**
 * @File:    flexible_button_demo.c
 * @Author:  MurphyZhao
 * @Date:    2018-09-29
 *
 * Copyright (c) 2018-2019 MurphyZhao <d2014zjt@163.com>
 *               https://github.com/murphyzhao
 * All rights reserved.
 * License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Change logs:
 * Date        Author       Notes
 * 2018-09-29  MurphyZhao   First add
 * 2019-08-02  MurphyZhao   迁移代码到 murphyzhao 仓库
*/

#include "flexible_button.h"
#include <board.h>
#include "key_init.h"

flex_button_t user_button[USER_BUTTON_MAX];

uint8_t i;

key_flag_struct key_flag;

void btn_0_cb(flex_button_t *btn)
{
    switch (btn->event)
    {
    case FLEX_BTN_PRESS_DOWN:

        if(user_button[1].event==FLEX_BTN_PRESS_DOWN)
        {
            key_flag.key_0_1_meantime_flag=1;
        }
        break;
    case FLEX_BTN_PRESS_CLICK:

        key_flag.key_0_flag =1 ;

        break;
    case FLEX_BTN_PRESS_DOUBLE_CLICK:

        break;
    case FLEX_BTN_PRESS_SHORT_START:

        break;
    case FLEX_BTN_PRESS_SHORT_UP:

        break;
    case FLEX_BTN_PRESS_LONG_START:

        break;
    case FLEX_BTN_PRESS_LONG_UP:

        break;
    case FLEX_BTN_PRESS_LONG_HOLD:
        break;
    case FLEX_BTN_PRESS_LONG_HOLD_UP:

        break;
    }
}

void btn_1_cb(flex_button_t *btn)
{
    switch (btn->event)
    {
    case FLEX_BTN_PRESS_DOWN:

        if(user_button[0].event==FLEX_BTN_PRESS_DOWN)
        {
            key_flag.key_0_1_meantime_flag=1;
        }
        break;
    case FLEX_BTN_PRESS_CLICK:

        key_flag.key_1_flag =1 ;

        break;
    case FLEX_BTN_PRESS_DOUBLE_CLICK:

        break;
    case FLEX_BTN_PRESS_SHORT_START:

        break;
    case FLEX_BTN_PRESS_SHORT_UP:

        break;
    case FLEX_BTN_PRESS_LONG_START:

        break;
    case FLEX_BTN_PRESS_LONG_UP:

        break;
    case FLEX_BTN_PRESS_LONG_HOLD:
        break;
    case FLEX_BTN_PRESS_LONG_HOLD_UP:

        break;
    }
}

uint8_t button_key0_read(void)
{
	if(gui_flag.rotation_flag)
    return GPIO_ReadPin(CW_GPIOB,GPIO_PIN_1);
	else
		return GPIO_ReadPin(CW_GPIOA,GPIO_PIN_0);
}

uint8_t button_key1_read(void)
{
	if(gui_flag.rotation_flag)
    return GPIO_ReadPin(CW_GPIOA,GPIO_PIN_0);
	else
		return GPIO_ReadPin(CW_GPIOB,GPIO_PIN_1);
}

void user_button_init(void)
{
    int i;
    user_button[USER_BUTTON_0].usr_button_read = button_key0_read;
    user_button[USER_BUTTON_0].cb = (flex_button_response_callback)btn_0_cb;

    user_button[USER_BUTTON_1].usr_button_read = button_key1_read;
    user_button[USER_BUTTON_1].cb = (flex_button_response_callback)btn_1_cb;

    GPIO_InitTypeDef GPIO_InitStruct;  // GPIO初始化结构体
    GPIO_InitStruct.Pins = GPIO_PIN_0;           // GPIO引脚
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT_PULLUP;  // 上拉输入
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;     // 输入速度高
    GPIO_InitStruct.IT=GPIO_IT_NONE;
    GPIO_Init(CW_GPIOA, &GPIO_InitStruct);    // 初始化
    GPIO_InitStruct.Pins = GPIO_PIN_1;           // GPIO引脚
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT_PULLUP;  // 上拉输入
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;       // 输入速度高
    GPIO_InitStruct.IT=GPIO_IT_NONE;
    GPIO_Init(CW_GPIOB, &GPIO_InitStruct);		// 初始化

    for (i = 0; i < USER_BUTTON_MAX; i ++)
    {
        user_button[i].pressed_logic_level = 0;
        user_button[i].click_start_tick = 20;
        user_button[i].short_press_start_tick = 100;
        user_button[i].long_press_start_tick = 200;
        user_button[i].long_hold_start_tick = 300;

        flex_button_register(&user_button[i]);
    }
}
